package com.pro01.backend.controller;

import com.pro01.backend.pojo.*;
import com.pro01.backend.service.ShopItemService;
import com.pro01.backend.service.UserInviteService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/points/shop")
public class ShopController {

    @Autowired
    private ShopItemService shopItemService;

    @Autowired
    private UserInviteService userInviteService;

    /**
     * 用户商城首页：分页查看所有可兑换商品
     */
    @GetMapping
    public Result<PageResult<ShopItem>> getStoreItems(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {

        try {
            Long userId = TokenUtil.getUserId(request); // 校验登录
            PageResult<ShopItem> pageResult = shopItemService.getActiveItems(pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询商城商品失败：" + e.getMessage());
        }
    }

    // 用户兑换商品
    @PostMapping("/redeem")
    public Result<Void> redeemItem(@RequestBody Map<String, Object> requestBody,
                                   HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            Long itemId = ((Number) requestBody.get("itemId")).longValue();

            shopItemService.redeemItem(userId, itemId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("兑换失败：" + e.getMessage());
        }
    }

    // 用户显示自己兑换的邀请码
    @GetMapping("/invites")
    public Result<PageResult<UserInvite>> getUserInvites(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            PageResult<UserInvite> pageResult = userInviteService.getUserInvitesPaged(userId, pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取邀请码失败：" + e.getMessage());
        }
    }

    // 用户查看兑换记录
    @GetMapping("/orders")
    public Result<PageResult<ShopOrder>> getUserOrders(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {
        try {
            Long userId = TokenUtil.getUserId(request);
            PageResult<ShopOrder> pageResult = shopItemService.getUserOrders(userId, pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取兑换记录失败：" + e.getMessage());
        }
    }


}
